'
' *  Authors:  Benton Stark
' * 
' *  Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com) 
' *
' * Permission is hereby granted, free of charge, to any person obtaining a copy
' * of this software and associated documentation files (the "Software"), to deal
' * in the Software without restriction, including without limitation the rights
' * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' * copies of the Software, and to permit persons to whom the Software is
' * furnished to do so, subject to the following conditions:
' * 
' * The above copyright notice and this permission notice shall be included in
' * all copies or substantial portions of the Software.
' * 
' * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' * THE SOFTWARE.
' * 
' 


Imports System.Collections
Imports System.Collections.Generic
Imports System.Text


Namespace FTP
    ''' <summary>
    ''' Ftp response collection.
    ''' </summary>
    Public Class FtpFeatureArgumentCollection
        Implements IEnumerable(Of FtpFeatureArgument)
        Private _list As New List(Of FtpFeatureArgument)()
        Private _text As String

        ''' <summary>
        ''' Default constructor for no feature arguments.
        ''' </summary>

        Public Sub New()
        End Sub

        ''' <summary>
        ''' Default constructor with features.
        ''' </summary>
        ''' <param name="text">Raw feature list text.</param>
        Public Sub New(text As String)
            If [String].IsNullOrEmpty(text) Then
                Throw New ArgumentNullException("text")
            End If

            _text = text
            Parse()
        End Sub

        ''' <summary>
        ''' Searches for the specified object and return the zero-based index of the
        ''' first occurrence within the entire FtpFeatureArgumentCollection list.
        ''' </summary>
        ''' <param name="item">The FtpFeatureArgument object to locate in the collection.</param>
        ''' <return>The zero-based index of the first occurrence of item within the entire if found; otherwise, -1.</return>
        Public Function IndexOf(item As FtpFeatureArgument) As Integer
            Return _list.IndexOf(item)
        End Function

        ''' <summary>
        ''' Adds an FtpFeatureArgument to the end of the FtpFeatureArgumentCollection list.
        ''' </summary>
        ''' <param name="item">FtpFeatureArgument object to add.</param>
        Public Sub Add(item As FtpFeatureArgument)
            _list.Add(item)
        End Sub

        ''' <summary>
        '''  Gets the number of elements actually contained in the FtpFeatureArgumentCollection list.
        ''' </summary>
        Public ReadOnly Property Count() As Integer
            Get
                Return _list.Count
            End Get
        End Property

        Private Function GetEnumerator() As IEnumerator(Of FtpFeatureArgument) Implements IEnumerable(Of FtpFeatureArgument).GetEnumerator
            Return _list.GetEnumerator()
        End Function

        Private Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
            Return _list.GetEnumerator()
        End Function

        ''' <summary>
        ''' Gets an FtpFeatureArgument from the FtpFeatureArgumentCollection list based on index value.
        ''' </summary>
        ''' <param name="index">Numeric index of item to retrieve.</param>
        ''' <return>FtpFeatureArgument object.</return>
        Default Public ReadOnly Property Item(index As Integer) As FtpFeatureArgument
            Get
                Return _list(index)
            End Get
        End Property

        ''' <summary>
        ''' Gets an FtpFeature from the FtpFeatureCollection list based on name.
        ''' </summary>
        ''' <param name="name">Name of the feature.</param>
        ''' <return>FtpFeature object.</return>
        Default Public ReadOnly Property Item(name As String) As FtpFeatureArgument
            Get
                Return Find(name)
            End Get
        End Property

        ''' <summary>
        ''' Remove all elements from the FtpFeatureArgumentCollection list.
        ''' </summary>
        Public Sub Clear()
            _list.Clear()
        End Sub

        ''' <summary>
        ''' Get the raw FTP server supplied reponse text for features.
        ''' </summary>
        ''' <return>A string containing the FTP feature list.</return>
        Public Function GetRawText() As String
            Return _text
        End Function

        ''' <summary>
        ''' Linearly searches for the specified object based on the feature 'name' parameter value
        ''' and return the corresponding object with the name is found; otherwise null.  Search is case insensitive.
        ''' </summary>
        ''' <param name="name">The name of the FtpFeatureArgument to locate in the collection.</param>
        ''' <return>FtpFeatureArgument object if the name if found; otherwise null.</return>
        Public Function Find(name As String) As FtpFeatureArgument
            If [String].IsNullOrEmpty(name) Then
                Throw New ArgumentNullException("name", "must have a value")
            End If

            For Each item As FtpFeatureArgument In _list
                If [String].Compare(name, item.Name, True) = 0 Then
                    Return item
                End If
            Next

            Return Nothing
        End Function

        ''' <summary>
        ''' Linearly searches for the specified object based on the feature 'name' parameter value
        ''' and return true if an object with the name is found; otherwise false.  Search is case insensitive.
        ''' </summary>
        ''' <remarks>
        ''' example:  col.Contains("UTF8");
        ''' </remarks>
        ''' <param name="name">The name of the FtpFeatureArgument to locate in the collection.</param>
        ''' <return>True if the name if found; otherwise false.</return>
        Public Function Contains(name As String) As Boolean
            Return If(Find(name) IsNot Nothing, True, False)
        End Function

        Private Sub Parse()
            Dim args As String() = GetArgumentArray()
            For Each arg As String In args
                _list.Add(New FtpFeatureArgument(arg))
            Next
        End Sub

        ''' <summary>
        ''' Gets the FTP feature arguments as a string array.
        ''' </summary>
        ''' <return>Array of strings containing arguments; otherwise null.</return>
        Public Function GetArgumentArray() As String()
            If [String].IsNullOrEmpty(_text) Then
                Return Nothing
            ElseIf _text.Contains(";") Then
                Return SplitSemi(_text)
            ElseIf _text.Contains(" ") Then
                Return SplitSpace(_text)
            Else
                Dim a As String() = New String(0) {}
                a(0) = _text
                Return a
            End If
        End Function

        Private Function SplitSemi(list As String) As String()
            Return list.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries)
        End Function

        Private Function SplitSpace(list As String) As String()
            Return list.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
        End Function



    End Class
End Namespace
